<div class="layui-form seller-form" action="">
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">文章标题：</label>
            <div class="layui-input-inline">
                <input type="text" name="title" lay-verify="title" placeholder="请输入标题关键字" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">文章分类：</label>
            <div class="layui-input-inline">
                <select name="type_id" id="type" lay-verify="">
                    <option value="">请选择文章类型</option>
                    {volist name='typeList' id='vo'}
                    <option value="{$vo.id}">{$vo.type_name}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">发布状态：</label>
            <div class="layui-input-inline" style="width: 120px;">
                <select name="is_pub" lay-verify="">
                    <option value=""></option>
                    <option value="1">已发布</option>
                    <option value="2">未发布</option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">更新时间：</label>
            <div class="layui-input-inline">
                <input type="text" id="utime" name="utime" lay-verify="title" placeholder="请选择时间段" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="article_search"><i class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add-article"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>
    </div>
</div>



<div class="table-body">
    <table id="articleTable" lay-filter="articleTable"></table>
</div>
<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function() {
        var layer = layui.layer,
            table = layui.table,
            form = layui.form,
            date = layui.laydate;
        //执行渲染
        table.render({
            elem: '#articleTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-300',
            cellMinWidth: '80',
            page: 'true',
            limit: '20',
            id: 'articleTable',
            url: "{:url('Article/index')}",
            cols: [
                    [ //标题栏
                        {
                            type: 'numbers'
                        }, {
                            field: 'is_rec',
                            title: '是否推荐',
                            align: 'center',
                            templet: "#is_rec",
                            width: 100
                        }, {
                            field: 'is_hot',
                            title: '是否热门',
                            align: 'center',
                            templet: "#is_hot",
                            width: 100
                        }, {
                            field: 'is_pub',
                            title: '是否发布',
                            align: 'center',
                            templet: "#is_pub",
                            width: 100
                        }, {
                            field: 'title',
                            title: '文章标题'
                        }, {
                            field: 'img',
                            title: '文章封面',
                            align: 'center',
                            width: 120,
                            templet: function(data) {
                                return '<a href="javascript:void(0);" onclick=viewImage("' + data.cover + '")><image style="max-width:30px;max-height:30px;" src="' + data.cover + '"/></a>';
                            }
                        }, {
                            field: 'type_name',
                            title: '文章分类',
                            align: 'center'
                        }, {
                            field: 'codes',
                            title: '文章类型',
                            align: 'center'
                        }, {
                            field: 'read_amount',
                            title: '阅读量',
                            align: 'center',
                            width: 100
                        }, {
                            field: 'ctime',
                            title: '创建时间',
                            align: 'center',
                            sort: true
                        }, {
                            field: 'utime',
                            title: '更新时间',
                            align: 'center',
                            sort: true
                        }, {
                            field: 'sort',
                            title: '排序',
                            align: 'center',
                            sort: true,
                            width: 100
                        }, {
                            fixed: 'right',
                            width: 150,
                            title: '操作',
                            align: 'center',
                            toolbar: '#articleBar'
                        }
                    ]
                ] //设置表头
                //,…… //更多参数参考右侧目录：基本参数选项
        });

        //搜索
        form.on('submit(article_search)', function(data) {
            layui.table.reload('articleTable', {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        date.render({
            elem: '#ctime',
            range: '~'
        });

        date.render({
            elem: '#utime',
            range: '~'
        });


        //layui弹出层
        $(document).on('click', '.add-article', function() {
            window.location.href = "{:url('Article/add')}";
        });


        //监听工具条
        table.on('tool(articleTable)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if (layEvent === 'del') { //删除
                layer.confirm('真的要删除么', {
                    icon: 3
                }, function(index) {
                    JsGet("{:url('Article/del')}?id=" + data.id, function(res) {
                        if (res.status) {
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index); //向服务端发送删除指令
                        }
                        layer.msg(res.msg);
                    })
                });
            } else if (layEvent === 'edit') { //编辑
                window.location.href = "{:url('Article/edit')}?id=" + data.id;
            }
        });


        // 监听操作状态
        form.on('switch(state)', function(obj) {
            JsPost("{:url('Article/changeState')}", {
                id: obj.value,
                elem: obj.elem.name,
                state: obj.elem.checked
            }, function(res) {
                layer.msg(res.msg)
            })
        });

    })
</script>


<script type="text/html" id="is_rec">
    <input type="checkbox" name="rec" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="state" {{ d.is_rec==1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="is_hot">
    <input type="checkbox" name="hot" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="state" {{ d.is_hot==1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="is_pub">
    <input type="checkbox" name="pub" value="{{d.id}}" lay-skin="switch" lay-text="已发布|未发布" lay-filter="state" {{ d.is_pub==1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="articleBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>